<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="商品名称" prop="goodsName">
        <el-input v-model="queryParams.goodsName" placeholder="请输入商品名称" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="商品品牌" prop="brandId">
        <el-select v-model="queryParams.brandId" placeholder="请选择下拉选择" clearable :style="{width: '100%'}">
          <el-option v-for="(item, index) in listTTerminalBrandInfo" :key="index" :label="item.brand" :value="item.id"
            :disabled="item.disabled"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['pos:TTerminalGoodsInfo:add']">新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['pos:TTerminalGoodsInfo:edit']">修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['pos:TTerminalGoodsInfo:remove']">删除
        </el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['pos:TTerminalGoodsInfo:export']"
        >导出
        </el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="TTerminalGoodsInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column label="主键id" align="center" prop="id" v-if="false" />
      <el-table-column label="商品品牌" align="center" prop="brandIdShow" />
      <el-table-column label="商品图片" align="center" prop="goodsImg">
        <template slot-scope="scope">
          <el-popover placement="left" trigger="click" width="400">
            <img :src="scope.row.goodsImg" width="100%" />
            <img slot="reference" :src="scope.row.goodsImg" :alt="scope.row.goodsImg"
              style="max-height: 70px;max-width: 70px; padding:5px" />
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="商品金额类型" align="center" prop="goodsStatus" />
      <el-table-column label="商品名称" align="center" prop="goodsName" />
      <el-table-column label="购买金额" align="center" prop="amount">
        <template slot-scope="scope">
          {{scope.row.amount}}{{scope.row.exchangeUnit}}/{{scope.row.unit}}
        </template>
      </el-table-column>
      <el-table-column label="商品单位" align="center" prop="unit" />
      <el-table-column label="商品排序" align="center" prop="sort" />
      <el-table-column label="商品状态" align="center" prop="isValid">
        <template slot-scope="scope">
          <p v-if="scope.row.isValid==0">正常</p>
          <p v-if="scope.row.isValid==1">禁用</p>
        </template>
      </el-table-column>
      <!--<el-table-column label="文字介绍" align="center" prop="describe" :show-overflow-tooltip="true"/>-->
      <el-table-column label="产品兑换说明(兑换商城)" align="center" prop="conversionExplain" :show-overflow-tooltip="true"
        v-if="false" />
      <!-- <el-table-column label="产品说明" align="center" prop="goodsExplain" :show-overflow-tooltip="true"/> -->
      <el-table-column label="是否热门推荐" align="center" prop="isPopular">
        <template slot-scope="scope">
          <p v-if="scope.row.isPopular==0">热门</p>
          <p v-if="scope.row.isPopular==1">非热门</p>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-plus" @click="addSn(scope.row)"
            v-hasPermi="['pos:TTerminalGoodsInfo:addTerminalSn']">添加终端号
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['pos:TTerminalGoodsInfo:edit']">修改
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['pos:TTerminalGoodsInfo:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />


    <!-- 添加或修改商品规格对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="商品id" prop="goodsId" v-if="false">
          <el-input v-model="form.goodsId" placeholder="商品id" />
        </el-form-item>
        <el-form-item label="商品品牌id" prop="goodsId" v-if="false">
          <el-input v-model="form.brandId" placeholder="商品品牌id" />
        </el-form-item>
        <el-form-item label="商品名称" prop="terminalGmv">
          <el-input v-model="form.goodsName" placeholder="商品名称" disabled />
        </el-form-item>
        <!--        <el-form-item label="输入SN码方式">
          <el-radio-group v-model="form.isValid">
            <el-radio label="0">逐条输入</el-radio>
            <el-radio label="1">批量输入</el-radio>
          </el-radio-group>
        </el-form-item>-->
        <el-form-item label="机具SN码列表" prop="deliveryFee" v-if="form .isValid == 0 ? true:false" wrap="soft|warp">
          <el-input v-model="form.snCode" type="textarea" :autosize="{minRows: 6, maxRows: 4}" :style="{width: '100%'}">
          </el-input>
          <div style="font-size: 12px;"><span style="color: red;">*</span>请在每条SN码后换行继续输入</div>
        </el-form-item>
<!--        <el-form-item label="SN码列表" prop="snCodes">
          <el-input v-model="form.snCodes" type="textarea" placeholder="请输入SN码,多个sn码请用 逗号 分割" />
        </el-form-item>-->
        <el-form-item label="终端已有交易额" prop="terminalGmv">
          <el-input v-model="form.terminalGmv" placeholder="终端已有交易额" />
        </el-form-item>
<!--        <el-form-item label="绑定时间" prop="bindingTime">-->
<!--          <el-date-picker v-model="form.bindingTime" style="width: 100%;" value-format="yyyy-MM-dd HH:mm:ss" type="datetime" placeholder="选择绑定日期时间">-->
<!--          </el-date-picker>-->
<!--          &lt;!&ndash; <el-input v-model="form.bindingTime" placeholder="绑定时间"/> &ndash;&gt;-->
<!--        </el-form-item>-->
        <!--        <el-form-item label="机具SN码列表" prop="deliveryFee" v-if="form .isValid == 1 ? true:false">
          <el-input v-model="form.beginSnCode" placeholder="开始编码"/>
          <el-input v-model="form.endSnCode" placeholder="结束编码"/>
        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-preventReClick>确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>


</template>

<script>
  import {
    addTTerminalInfo,
    delTTerminalGoodsInfo,
    exportTTerminalGoodsInfo,
    listTTerminalBrandInfo,
    listTTerminalGoodsInfoTerminal,
    addTerminalSn
  } from "@/api/pos/TTerminalGoodsInfo";

  export default {
    name: "TTerminalGoodsInfo",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 导出遮罩层
        exportLoading: false,
        // 选中数组
        ids: [],
        name: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 商品基础表格数据
        TTerminalGoodsInfoList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          brandId: null,
          goodsType: 0,
          goodsStatus: null,
          goodsName: null,
          amount: null,
          deliveryFee: null,
          unit: null,
          sort: null,
          isValid: null,
          describe: null,
          conversionExplain: null,
          goodsExplain: null,
          isPopular: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {},
        listTTerminalBrandInfo: [],
      };
    },
    created() {
      this.getList();
      listTTerminalBrandInfo().then(response => {
        console.log(response)
        this.listTTerminalBrandInfo = response;
      })
    },
    methods: {
      /** 查询商品基础列表 */
      getList() {
        this.loading = true;
        listTTerminalGoodsInfoTerminal(this.queryParams).then(response => {
          this.TTerminalGoodsInfoList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.name = selection.map(item => item.goodsName)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        // const tableId = row.tableId || this.ids[0];
        this.$router.push({
          path: '/TTerminalGoodsInfo/terminal/add_terminal',
          query: {}
        });
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        const id = row.id || this.ids
        this.$router.push({
          path: '/TTerminalGoodsInfo/terminal/edit_terminal',
          query: {
            "id": id
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        const name = row.goodsName || this.name
        this.$modal.confirm('是否确认删除？').then(function() {
          return delTTerminalGoodsInfo(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$modal.confirm('是否确认导出所有商品基础数据项？').then(() => {
          this.exportLoading = true;
          return exportTTerminalGoodsInfo(queryParams);
        }).then(response => {
          this.$download.name(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
      },
      addSn(row) {
        this.reset();
        console.log(row)
        this.form = row;
        this.form.goodsId = row.id; //商品id
        this.form.terminalModel = row.goodsName; //商品名称
        this.open = true;
        this.title = "添加终端号";
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      /** 提交按钮 */
      submitForm(event) {
        //console.log("-----"+this.el.disabled());
        //var el = event.currentTarget;
        //alert("当前对象的内容："+el.innerHTML.);
        //return;
        this.$refs["form"].validate(valid => {
          addTerminalSn(this.form).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            //el.disabled = false;
            this.getList();
          });
        });
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          brandId: null,
          goodsName: null,
          amount: null,
          isValid: "0",
          deliveryFee: null,
          beginSnCode: null,
          endSnCode: null,
          terminalModel: null
        };
        this.resetForm("form");
      },
    }
  };
</script>
